package  birdry.utils	{
	
	//	import birdry.utils.MovieClipUtil;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.FrameLabel;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.utils.Dictionary;
	import flash.events.MouseEvent;
	
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	import flash.utils.setTimeout;
	
	public class MovieClipUtil	extends Sprite	{
		
		public static var gotoFrametargetFrameDic:Dictionary = new Dictionary(true);
		public static var gotoFrame_completeDic:Dictionary = new Dictionary(true);
		
		/**
		 * MovieClipUtil.addButtonMode( bt, onBtClick);
			function onBtClick(e:MouseEvent):void 		{
				trace( "e : " + e );
			}
		 * @param	bt
		 * @param	listener
		 * @param	hit
		 */
		public static function addButtonMode(bt:MovieClip, listener:Function, hit:DisplayObject = null): void	{
			if (hit == null )		bt.buttonMode = true;
			var obj:* = (hit == null) ? bt : hit;
			obj.addEventListener( MouseEvent.MOUSE_OVER, onMouseOver);
			obj.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut);
			obj.addEventListener( MouseEvent.CLICK, onClick);
			function onMouseOver(e:MouseEvent):void 		{
				var mc:MovieClip = (hit == null) ? MovieClip(e.currentTarget) : MovieClip(e.currentTarget.parent);
				gotoFrame(mc);
			}
			function onMouseOut(e:MouseEvent):void 		{
				var mc:MovieClip = (hit == null) ? MovieClip(e.currentTarget) : MovieClip(e.currentTarget.parent);
				gotoFrame(mc, 1);
			}
			function onClick(e:MouseEvent):void 		{
				listener(e);
			}
		}
		
		public static function returnFindMcByInstanceName( mc:MovieClip, findName:String ): MovieClip 		{
			var returnMc:MovieClip;
			var tempMc:*;
			var childrenMax:uint = mc.numChildren;
			for ( var i:uint = 0; i < childrenMax; ++i)	{
				tempMc = mc.getChildAt(i);
				if (tempMc.name == findName)	return tempMc;
				if ( tempMc is DisplayObjectContainer )	returnFindMcByInstanceName( tempMc, findName );
			}
			return null;
		}
		
		
		//------------------------ gotoFrame ---------------------//
		/**/
		public static function gotoFrame(mc:MovieClip, targetFrame:* = 0, complete:Function = null):void 	{
			if (targetFrame is String)	{
				targetFrame = returnLabelFrame(mc, targetFrame);
			}
			gotoFrametargetFrameDic[mc] = (targetFrame == 0) ? mc.totalFrames : targetFrame;
			gotoFrame_completeDic[mc] = complete;
			mc.removeEventListener(Event.ENTER_FRAME, gotoFrame_Handler);
			mc.addEventListener(Event.ENTER_FRAME, gotoFrame_Handler);
		};
		
		private static function gotoFrame_Handler(e:Event): void	{
			var mc:MovieClip = e.currentTarget as MovieClip;
			var targetFrame:int = gotoFrametargetFrameDic[mc];
			var complete:Function = gotoFrame_completeDic[mc];
			
			if (mc.currentFrame < targetFrame)	{
					mc.nextFrame();
			}	else if (mc.currentFrame > targetFrame)	{
					mc.prevFrame();
			}	else	{
				mc.stop();
				mc.removeEventListener(Event.ENTER_FRAME, gotoFrame_Handler);
				delete gotoFrametargetFrameDic[mc];
				delete gotoFrame_completeDic[mc];
				if (complete != null)	complete();
			}
		}
		
		public static function searchFrame(mc:MovieClip, func:Function, targetFrame:*): void	{
			if (!targetFrame)	{
				targetFrame = mc.totalFrames;
			}	else		{
				if (targetFrame is String)	{
					trace("targetFrame : " + targetFrame);
					targetFrame = returnLabelFrame(mc, targetFrame);
					trace("targetFrame : " + targetFrame);
				}
			}
			
			mc.addEventListener(Event.ENTER_FRAME, onEnter);
			function onEnter( e: Event ): void	{
				if (mc.currentFrame == targetFrame)	{
					func();
					mc.removeEventListener(Event.ENTER_FRAME, onEnter);
				}
			}
		}
		
		public static function returnLabelFrame(mc:MovieClip, strLabel:String): uint	{
			var returnFrame:uint = 0;
			var labels:Array = mc.currentLabels;
			for (var i:uint = 0; i < labels.length; i++) {
				var frameLabel:FrameLabel = labels[i];
				if (frameLabel.name == strLabel)	{
					returnFrame = frameLabel.frame;
				}
			}
			return returnFrame;
		}
		
		public static function allPlay(mc:*): void	{
			if(mc is MovieClip)	MovieClip(mc).play();
			for ( var i:int = mc.numChildren - 1; i >= 0; i-- )	{
				if (mc.getChildAt(i) is DisplayObjectContainer)	allPlay(mc.getChildAt(i));
			}
		}
		public static function allStop(mc:*): void	{
			if(mc is MovieClip)	MovieClip(mc).stop();
			for ( var i:int = mc.numChildren - 1; i >= 0; i-- )	{
				if (mc.getChildAt(i) is DisplayObjectContainer)	allStop(mc.getChildAt(i));
			}
		}
		
	}
}

